﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PGMHelper;

namespace CouplingBeamProject
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Struct = new Structure();
            this.StoryInfoDataGridView.SetApperance(DataGridViewSelectionMode.FullRowSelect);
            this.tabControl1.SetApperance();
        }

        /// <summary>
        /// 带拉缝模型
        /// </summary>
        private Structure Struct { set; get; }

        /// <summary>
        /// 带拉缝模型建立
        /// </summary>
        private Structure SetCoupledShearWallWithInfillModel()
        {
            #region 输入参数
            double LeftWallWidth = Convert.ToDouble(CSWWILeftWallWidthTextBox.Text);
            double MiddleBeamWidth = Convert.ToDouble(CSWWIMiddleBeamWidthTextBox.Text);
            double RightWallWidth = Convert.ToDouble(CSWWIRightWallWidthTextBox.Text);
            double MiddleBeamHeight = Convert.ToDouble(CSWWIMiddleBeamHeightTextBox.Text);
            double StoreyHeight = Convert.ToDouble(CSWWIStoreyHeightTextBox.Text);
            double LeftInfillWidth = Convert.ToDouble(CSWWILeftInfillWidthTextBox.Text);
            double RightInfillWidth = Convert.ToDouble(CSWWIRightInfillWidthTextBox.Text);
            double DownInfillHeight = Convert.ToDouble(CSWWIDownInfillHeightTextBox.Text);
            int StoreyNum = Convert.ToInt32(CSWWIStoreyNumTextBox.Text);
            #endregion
            //建立拉缝模型
            var structure  = new Structure();
            for (int i = 0; i < StoreyNum; i++)
            {
                structure.AddStoreyInfo(LeftWallWidth, MiddleBeamWidth, RightWallWidth, MiddleBeamHeight, 
                StoreyHeight, LeftInfillWidth, RightInfillWidth, DownInfillHeight);
            }
            return structure;
        }

        /// <summary>
        /// 联肢剪力墙模型建立
        /// </summary>
        private Structure SetCoupledShearWallModel()
        {
            #region 输入参数
            double LeftWallWidth = Convert.ToDouble(CSWLeftWallWidthTextBox.Text);
            double MiddleBeamWidth = Convert.ToDouble(CSWMiddleBeamWidthTextBox.Text);
            double RightWallWidth = Convert.ToDouble(CSWRightWallWidthTextBox.Text);
            double MiddleBeamHeight = Convert.ToDouble(CSWMiddleBeamHeightTextBox.Text);
            double StoreyHeight = Convert.ToDouble(CSWStoreyHeightTextBox.Text);
            int StoreyNum = Convert.ToInt32(CSWStoreyNumTextBox.Text);
            double MiddleBeamSizeFactor = Convert.ToDouble(MiddleBeamSizeFactorTextBox.Text);
            #endregion
            //建立联肢剪力墙模型
            var CoupledShearWallStructure = new Structure();
            for (int i = 0; i < StoreyNum; i++)
            {
                CoupledShearWallStructure.AddStoreyInfo(LeftWallWidth, MiddleBeamWidth, RightWallWidth, MiddleBeamHeight,
                StoreyHeight);
            }
            var CoupledShearWallStruc = new Structure(CoupledShearWallStructure, MiddleBeamHeight * MiddleBeamSizeFactor);
            return CoupledShearWallStruc;
        }

        /// <summary>
        /// PG1 求解位移和等效梁高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetDorEButton_Click(object sender, EventArgs e)
        {
            GlobalVar.Initial();
            double MiddleBeamSizeFactor = Convert.ToDouble(MiddleBeamSizeFactorTextBox.Text);
            var structure = ((Control)sender).Name.Contains("P2") ? this.Struct :
                ((Control)sender).Name.Contains("CSWWI") ?
                this.SetCoupledShearWallWithInfillModel() : this.SetCoupledShearWallModel();
            var factor = ((Control)sender).Name.Contains("CSWWI") ? 1 : MiddleBeamSizeFactor;
            //求解
            bool isGetD = ((Control)sender).Name.Contains("GetD");
            if (isGetD)
            {
                var value = structure.GetMaxResponse(factor);
                MessageBoxExtern.Asterisk(string.Format("顶点位移(mm): {0}", value));
            }
            else
            {
                var value = structure.GetEquivalResult();
                MessageBoxExtern.Asterisk(string.Format("等效梁尺寸放大系数:{0}", value));
            }
        }

        /// <summary>
        /// 添加楼层信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddStoreyButton_Click(object sender, EventArgs e)
        {
            this.ModelBasicInfoGroupbox.Enabled = false;
            #region 输入参数
            double LeftWallWidth = Convert.ToDouble(P2CSWWILeftWallWidthTextBox.Text);
            double MiddleBeamWidth = Convert.ToDouble(P2CSWWIMiddleBeamWidthTextBox.Text);
            double RightWallWidth = Convert.ToDouble(P2CSWWIRightWallWidthTextBox.Text);
            double MiddleBeamHeight = Convert.ToDouble(P2CSWWIMiddleBeamHeightTextBox.Text);
            double StoreyHeight = Convert.ToDouble(P2CSWWIStoreyHeightTextBox.Text);
            double LeftInfillWidth = Convert.ToDouble(P2CSWWILeftInfillWidthTextBox.Text);
            double RightInfillWidth = Convert.ToDouble(P2CSWWIRightInfillWidthTextBox.Text);
            double DownInfillHeight = Convert.ToDouble(P2CSWWIDownInfillHeightTextBox.Text);
            #endregion
            //建立拉缝模型
            Struct.AddStoreyInfo(LeftWallWidth, MiddleBeamWidth,
                RightWallWidth, MiddleBeamHeight, StoreyHeight, LeftInfillWidth, RightInfillWidth, DownInfillHeight);
            StoryInfoDataGridView.AddDatas(Struct.StoreyData);
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearButton_Click(object sender, EventArgs e)
        {
            this.ModelBasicInfoGroupbox.Enabled = true;
            StoryInfoDataGridView.AddDatas(Struct.Clear());
        }

        /// <summary>
        /// 求解等效梁尺寸放大系数并保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CalEquilbrumBeamSizeFactorButton_Click(object sender, EventArgs e)
        {
            //var multiStruct = new MultiStruct(GlobalVar.BeamSpanMin, GlobalVar.BeamSpanMax, GlobalVar.BeamSpanDelta, GlobalVar.BeamHeightMin, GlobalVar.BeamHeightMax, GlobalVar.BeamHeightDelta,
            //    GlobalVar.StoreyHeightMin, GlobalVar.StoreyHeightMax, GlobalVar.StoreyHeightDelta, GlobalVar.LeftWallWidthMin, GlobalVar.LeftWallWidthMax, GlobalVar.LeftWallWidthDelta,
            //    GlobalVar.WallRatioMin, GlobalVar.WallRatioMax, GlobalVar.WallRatioDelta, GlobalVar.StoreyNum);
            GlobalVar.Initial();
            //层高和楼层数目
            double StoreyHeight = Convert.ToDouble(P3StoreyHeightTextBox.Text);
            int StoreyNum = Convert.ToInt32(P3StoreyNumTextBox.Text);   
            //定义类
            var multiStruct = new MultiStruct(GlobalVar.BeamSpanMin, GlobalVar.BeamSpanMax, GlobalVar.BeamSpanDelta, GlobalVar.BeamHeightMin, GlobalVar.BeamHeightMax, GlobalVar.BeamHeightDelta,
                GlobalVar.LeftWallWidthMin, GlobalVar.LeftWallWidthMax, GlobalVar.LeftWallWidthDelta,
                GlobalVar.WallRatioMin, GlobalVar.WallRatioMax, GlobalVar.WallRatioDelta, StoreyHeight, StoreyNum);
            //批量计算
            multiStruct.Calculate();
            //保存文件
            multiStruct.ToFile();
        }

        /// <summary>
        /// 求解顶点位移并保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DispCalButton_Click(object sender, EventArgs e)
        {
            GlobalVar.Initial();
            //层高和楼层数目
            double StoreyHeight = Convert.ToDouble(P3StoreyHeightTextBox.Text);
            int StoreyNum = Convert.ToInt32(P3StoreyNumTextBox.Text);
            //定义类
            var multiStruct = new MultiStruct(GlobalVar.BeamSpanMin, GlobalVar.BeamSpanMax, GlobalVar.BeamSpanDelta, GlobalVar.BeamHeightMin, GlobalVar.BeamHeightMax, GlobalVar.BeamHeightDelta,
                GlobalVar.LeftWallWidthMin, GlobalVar.LeftWallWidthMax, GlobalVar.LeftWallWidthDelta,
                GlobalVar.WallRatioMin, GlobalVar.WallRatioMax, GlobalVar.WallRatioDelta, StoreyHeight, StoreyNum);
            multiStruct.CalculateDisp();
            multiStruct.ToDispFile();
        }
    }
}
